package co.yixiang.modules.activity.service.impl;

import co.yixiang.modules.activity.domain.YxStoreCouponProduct;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.activity.repository.YxStoreCouponProductRepository;
import co.yixiang.modules.activity.service.YxStoreCouponProductService;
import co.yixiang.modules.activity.service.dto.YxStoreCouponProductDto;
import co.yixiang.modules.activity.service.dto.YxStoreCouponProductQueryCriteria;
import co.yixiang.modules.activity.service.mapper.YxStoreCouponProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.annotation.Resource;


/**
* @author Walert
* @date 2020-03-06
*/
@Service
//@CacheConfig(cacheNames = "yxStoreCouponProduct")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCouponProductServiceImpl implements YxStoreCouponProductService {

    @Resource
    private YxStoreCouponProductRepository yxStoreCouponProductRepository;
    @Resource
    private YxStoreCouponProductMapper yxStoreCouponProductMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxStoreCouponProductQueryCriteria criteria, Pageable pageable){
        Page<YxStoreCouponProduct> page = yxStoreCouponProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxStoreCouponProductMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxStoreCouponProductDto> queryAll(YxStoreCouponProductQueryCriteria criteria){
        return yxStoreCouponProductMapper.toDto(yxStoreCouponProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxStoreCouponProductDto findById(Integer id) {
        YxStoreCouponProduct yxStoreCouponProduct = yxStoreCouponProductRepository.findById(id).orElseGet(YxStoreCouponProduct::new);
        ValidationUtil.isNull(yxStoreCouponProduct.getId(),"YxStoreCouponProduct","id",id);
        return yxStoreCouponProductMapper.toDto(yxStoreCouponProduct);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxStoreCouponProductDto create(YxStoreCouponProduct resources) {
        return yxStoreCouponProductMapper.toDto(yxStoreCouponProductRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public List<YxStoreCouponProductDto> createList(List<YxStoreCouponProduct> resources) {
        return yxStoreCouponProductMapper.toDto(yxStoreCouponProductRepository.saveAll(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreCouponProduct resources) {
        YxStoreCouponProduct yxStoreCouponProduct = yxStoreCouponProductRepository.findById(resources.getId()).orElseGet(YxStoreCouponProduct::new);
        ValidationUtil.isNull( yxStoreCouponProduct.getId(),"YxStoreCouponProduct","id",resources.getId());
        yxStoreCouponProduct.copy(resources);
        yxStoreCouponProductRepository.save(yxStoreCouponProduct);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxStoreCouponProductRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<YxStoreCouponProductDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxStoreCouponProductDto yxStoreCouponProduct : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("优惠券ID", yxStoreCouponProduct.getCid());
            map.put("关联商品Id", yxStoreCouponProduct.getProductId());
            map.put("优惠券创建时间", yxStoreCouponProduct.getAddTime());
            map.put("是否删除", yxStoreCouponProduct.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}